package com.gxs.tga.poc.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gxs.tga.poc.domain.TgaSolution;
import com.gxs.tga.poc.domain.TgaSolutionTR;
import com.gxs.tga.poc.domain.exceptions.DuplicateTgaSolutionTR;
import com.gxs.tga.poc.domain.exceptions.TgaSolutionTRNotFound;
import com.gxs.tga.poc.repository.TgaSolutionTRRepository;

@Service
public class TgaSolutionTRServiceImpl implements TgaSolutionTRService
{

	@Resource
	private TgaSolutionTRRepository tgaSolutionTRRepository;

	@Override
	@Transactional
	public TgaSolutionTR create(TgaSolutionTR relationship) throws DuplicateTgaSolutionTR
	{
		TgaSolutionTR existingTR = this.findById(relationship.getTrRecordId() );
		if(existingTR != null)
		{
			throw new DuplicateTgaSolutionTR();
		}
		TgaSolutionTR createdRelationship = relationship;
		
		return tgaSolutionTRRepository.save(createdRelationship);
	}

	@Override
	@Transactional
	public TgaSolutionTR findById(String trRecordId)
	{
		return tgaSolutionTRRepository.findOne(trRecordId);
	}

	@Override
	@Transactional(rollbackFor = TgaSolutionTRNotFound.class)
	public TgaSolutionTR delete(String trRecordId) throws TgaSolutionTRNotFound
	{
		TgaSolutionTR deletedRelationship = tgaSolutionTRRepository.findOne(trRecordId);

		if (deletedRelationship == null)
			throw new TgaSolutionTRNotFound();

		tgaSolutionTRRepository.delete(deletedRelationship);
		return deletedRelationship;
	}

	@Override
	@Transactional
	public List<TgaSolutionTR> findAll()
	{
		return tgaSolutionTRRepository.findAll();
	}

	@Override
	@Transactional(rollbackFor=TgaSolutionTRNotFound.class)
	public TgaSolutionTR update(TgaSolutionTR relationship) throws TgaSolutionTRNotFound 
	{
		TgaSolutionTR updatedRelationship = tgaSolutionTRRepository.findOne(relationship.getTrRecordId());
		
		if (updatedRelationship == null)
			throw new TgaSolutionTRNotFound();

		updatedRelationship.setAprf(relationship.getAprf());
		updatedRelationship.setCompId(relationship.getCompId());
		updatedRelationship.setTgaSolution(relationship.getTgaSolution());
		updatedRelationship.setItineraryName(relationship.getItineraryName());
				
		return updatedRelationship;
	}

	@Override
	@Transactional(readOnly=true)
	public List<TgaSolutionTR> findByCompId(String compId)
	{
		return tgaSolutionTRRepository.findByCompId(compId);	
	}

	@Override
	@Transactional(readOnly=true)
	public List<TgaSolutionTR> findByCompIdAndAprf(String compId, String aprf)
	{
		return tgaSolutionTRRepository.findByCompIdAndAprf(compId, aprf);	
	}
	@Override
	@Transactional
	public List<TgaSolutionTR> findByCompIdAndAprfAndTgaSolution(String compId, String aprf, TgaSolution tgaSolution)
	{
		return this.tgaSolutionTRRepository.findByCompIdAndAprfAndTgaSolution(compId, aprf, tgaSolution);
		
	}

	@Override
	@Transactional
	public List<TgaSolutionTR> findByTgaSolution(TgaSolution tgaSolution)
	{
		return this.tgaSolutionTRRepository.findByTgaSolution(tgaSolution);
		
	}
}
